import datetime
import json
import logging
import os
import subprocess
import threading
import time
from pathlib import Path
from typing import Union, List
from types import SimpleNamespace
import psutil
from hypium import UiDriver, UiComponent
from hypium.uidriver.by import By, BY
from hypium.uidriver.interface.uitree import IUiComponent

explore_dict = {}

src_path = os.path.dirname(os.path.dirname(__file__))


def get_src_path():
    return Path(src_path).parent


def run_cmd(cmd, cmd_dir):
    try:
        cmd_dir = os.path.abspath(cmd_dir)
        result = subprocess.run(
            cmd,
            shell=True,
            cwd=cmd_dir,
            check=True,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            text=True
        )
        return result.returncode, result.stdout, result.stderr
    except subprocess.CalledProcessError as e:
        return e.returncode, e.stdout, e.stderr
    except Exception as e:
        return None, None, str(e)
